package com.kawa.kemanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kawa.kemanage.entity.SystemNotice;
import com.kawa.kemanage.entity.User;
import com.kawa.kemanage.mapper.SystemNoticeMapper;
import com.kawa.kemanage.service.SystemNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class SystemNoticeImpl implements SystemNoticeService {
    @Autowired
    private SystemNoticeMapper systemNoticeMapper;

    @Override
    public List<com.kawa.kemanage.entity.SystemNotice> selectAll() {
        return systemNoticeMapper.selectAll();
    }

//    @Override
//    public String searchNotice(String userName) {
//        return systemNoticeMapper.searchNotice(userName);
//    }
//    @Override
//    public String searchNotice(String userName) {
//        try {
//            return systemNoticeMapper.searchNotice(userName);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null; // 或抛出业务异常
//        }
//    }
    @Override
    public List<User> searchNotice(String userName) {
        return systemNoticeMapper.searchNotice(userName);
    }

    @Override
    public boolean addNotice(SystemNotice systemNotice) {
        return  (systemNoticeMapper.addNotice(systemNotice) > 0);
    }

    @Override
    public boolean updateNotice(SystemNotice systemNotice) {
        return (systemNoticeMapper.updateNotice(systemNotice) > 0);
    }

    @Override
    public int updateNewsStatusAndReadTime(Integer notificationId, Date readTime, String status) {
        return systemNoticeMapper.updateNewsStatusAndReadTime(notificationId, readTime, status);
    }


    @Override
    public int deleteNotice(Integer notificationId) {
        int resultNum = 0;
        try {
            resultNum = systemNoticeMapper.deleteNotice(notificationId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultNum;
    }

    /**
     * 根据用户id查询评论消息
     * @param user_id
     * @return
     */
    @Override
    public List<SystemNotice> selectNoticeByUserId(Integer user_id) {
        return systemNoticeMapper.selectNoticeByUserId(user_id);
    }

    /**
     * 根据用户id查询系统消息
     *
     * @param userId
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SystemNotice> selectByUserId(Integer userId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<SystemNotice> systemNotices = systemNoticeMapper.selectByUserId(userId);
        return new PageInfo<>(systemNotices);
    }
}
